package com.android.jay.lib_audio.view;

import android.animation.Animator;
import android.content.Context;
import android.os.Build;


import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.android.jay.lib_audio.R;
import com.android.jay.lib_audio.mediaplayer.core.AudioController;
import com.android.jay.lib_audio.mediaplayer.event.AudioLoadEvent;
import com.android.jay.lib_audio.mediaplayer.event.AudioPauseEvent;
import com.android.jay.lib_audio.mediaplayer.event.AudioStartEvent;
import com.android.jay.lib_audio.mediaplayer.model.AudioBean;
import com.android.jay.lib_audio.view.adapter.MusicPagerAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.RequiresApi;
import androidx.viewpager.widget.ViewPager;

/**
 * Created by wzp on 2020/2/1 0001.
 * <p>
 * 描述  ：todo
 */
public class IndictorView extends RelativeLayout implements ViewPager.OnPageChangeListener {

    private Context mContext;

    /**
     * view
     **/
    private ImageView mImageView;
    private ViewPager mViewPager;
    private MusicPagerAdapter mAdapter;

    /**
     * data
     **/
    private AudioBean mAudioBean;
    private ArrayList<AudioBean> mQueue;


    public IndictorView(Context context) {
        this(context, null);
    }

    public IndictorView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IndictorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        EventBus.getDefault().register(this);
        initData();
    }

    private void initData() {
        mQueue = AudioController.getInstance().getQueue();
        mAudioBean = AudioController.getInstance().getNowPlaying();

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        initView();
    }

    private void initView() {
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.indictor_view, this);
        mImageView = rootView.findViewById(R.id.tip_view);
        mViewPager = rootView.findViewById(R.id.view_pager);
        mViewPager.setOverScrollMode(OVER_SCROLL_NEVER);
        mAdapter = new MusicPagerAdapter(mContext, mQueue);
        mViewPager.setAdapter(mAdapter);
        showLoadingView(false);
        //要在UI初始化完成 否则会多一次listener 响应
        mViewPager.addOnPageChangeListener(this);

    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        AudioController.getInstance().setPlayIndex(i);
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onPageScrollStateChanged(int i) {
        switch (i) {
            //滑动结束状态
            case ViewPager.SCROLL_STATE_IDLE:
                showPlayView();
                break;
            //滑动过程中
            case ViewPager.SCROLL_STATE_DRAGGING:
                showPauseView();
                break;
            //没有滑动
            case ViewPager.SCROLL_STATE_SETTLING:
                break;
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAudioLoadEvent(AudioLoadEvent event) {
        //更新viewpager为load状态
        mAudioBean = event.mAudioBean;
        showLoadingView(true);
    }


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAudioPauseEvent(AudioPauseEvent event) {
        //更新viewpager为暂停状态
        showPauseView();
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAudioStartEvent(AudioStartEvent event) {
        //更新viewpager为播放状态
        showPlayView();
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void showPauseView() {
        Animator animator = mAdapter.getAnim(mViewPager.getCurrentItem());
        if (animator != null) {
            animator.pause();
        }

    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void showPlayView() {
        Animator animator = mAdapter.getAnim(mViewPager.getCurrentItem());
        if (animator != null) {
            if (animator.isPaused()) {
                animator.resume();
            } else {
                animator.start();
            }
        }
    }

    private void showLoadingView(boolean isSmooth) {
        mViewPager.setCurrentItem(mQueue.indexOf(mAudioBean), isSmooth);
    }


}
